package cn.edu.nuc.onlinestore.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import UtilSever.Util;
import cn.edu.nuc.onlinestore.model.Carts;
import cn.edu.nuc.onlinestore.model.Client;
import cn.edu.nuc.onlinestore.model.Goods;

public class UserCartFrame extends JFrame {

	private JPanel contentPane;
	private JTable table;
	
	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserCartFrame frame = new UserCartFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/



	/**
	 * Create the frame.
	 */
	public UserCartFrame(Carts car) {
		setTitle(car.getUsername()+"的购物车详情");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 591, 377);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		
		
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(￥)");
		model.addColumn("数量");
		model.addColumn("总价格(￥)");
		//model.addColumn("操作");
		
		
		//遍历传经来的购物车并显示
		Map<Goods,Integer> goodlist=car.getBuygood();
		double total=car.gettheTotal();
		int totalnum=0;
		Set<Entry<Goods, Integer>> l=goodlist.entrySet();
		Iterator<Entry<Goods, Integer>> en=l.iterator();
		while(en.hasNext())
		{
			Map.Entry<Goods, Integer>  g=en.next();
			
			Goods good=g.getKey();
			int number=g.getValue();
			totalnum+=number;
			model.addRow(new String[]{""+good.getGoodsId(),good.getName(),""+good.getPeice(),""+number,""+number*good.getPeice()});
		}
		
		
		
		/*model.addRow(new String[]{"1","水杯","15.00","2","30.00"});
		model.addRow(new String[]{"5","农夫山泉","2.00","5","10"});
		model.addRow(new String[]{"6","毛巾","9.90","1","9.90"});*/
		table = new JTable( model );
		table.setBounds(10, 38, 543, 184);
		
		//JScrollPane pane = new JScrollPane( table );
		
		contentPane.add( table );
		
		JLabel label = new JLabel("总商品数量: "+totalnum);
		label.setBounds(195, 232, 116, 15);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("总金额: "+total);
		lblNewLabel.setBounds(342, 232, 211, 15);
		contentPane.add(lblNewLabel);
		
		JButton button = new JButton("结账");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Client c=Util.SeverSelectAClient(car.getUsername()).getObj();
				boolean d=Util.SeverdeleteCarts(c);
				if(d)
				{
					JOptionPane.showMessageDialog(null,"已结账,购物车清空","", JOptionPane.NO_OPTION);	
					UserGoodNumber.label.setText("购物车："+0);
				}else{
					JOptionPane.showMessageDialog(null,"失败","", JOptionPane.NO_OPTION);
				}
				setVisible(false);
				
			}
		});
		button.setBounds(456, 283, 93, 23);
		contentPane.add(button);
		
		
	}

}
